package com.songshuai.sales.client.controller;

import com.songshuai.sales.client.service.SalesMeetingService;
import com.songshuai.webapi.baseapi.dto.feign.request.pangu.salemeeting.QueryCustomersReqDto;
import com.songshuai.webapi.baseapi.dto.feign.request.pangu.salemeeting.SendInvitationReqDto;
import com.songshuai.webapi.baseapi.dto.feign.request.pangu.salemeeting.TalkNotesReqDto;
import com.songshuai.webapi.baseapi.dto.feign.response.pangu.salemeeting.*;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.utils.RequestParamValidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 销售-会议管理
 * @Author: lhx
 * @Date: 2019/3/23 14:10
 */
@Slf4j
@Api("销售-会议管理")
@RestController
@RequestMapping("/api/sales/meeting/")
public class SaleMeetingController extends BaseController{



    @Autowired
    private SalesMeetingService salesMeetingService;


    /**
     * 查询会议列表
     * @param type 0-最近 1-未来
     * @return
     */
    @ApiOperation("查询会议列表")
    @GetMapping("queryMeetingList")
    public CompletableFuture<QueryResponse<List<MeetingListResDto>>> queryMeetingList(@RequestParam("type")Integer type){

        String currentUserId = getCurrentUserId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(currentUserId)){
                    return salesMeetingService.queryMeetingList(type,currentUserId);
                }else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/sales/meeting/queryMeetingList", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(),e.getMessage());
            }
        });

    }


    /**
     * 根据会议查询客户集合（已邀约）
     * @return
     */
    @ApiOperation("根据会议查询客户集合")
    @GetMapping("queryCustomerList")
    public CompletableFuture<QueryResponse<List<CustomerListResDto>>> queryCustomerList(@RequestParam("code") String code) {
        String currentUserId = getCurrentUserId();
        return CompletableFuture.supplyAsync(() -> {
            try {
                if (StringUtils.isNotBlank(currentUserId)) {
                    return salesMeetingService.queryCustomerList(currentUserId, code);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/sales/meeting/queryCustomerList", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(), e.getMessage());
            }
        });
    }


    /**
     * 查看邀请函
     * @param code 会议编号
     * @return
     */
    @ApiOperation("查看邀请函")
    @GetMapping("queryInvitationLetter")
    public CompletableFuture<QueryResponse<List<InvitationLetterResDto>>> queryInvitationLetter(@RequestParam("code")String code){
        String currentUserId = getCurrentUserId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(currentUserId)){
                    return salesMeetingService.queryInvitationLetter(code);
                }else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/sales/meeting/queryInvitationLetter", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(),e.getMessage());
            }
        });
    }


    /**
     * 查询客户信息
     * 邀约客户，根据姓名或手机号模糊查询
     * @return
     */
    @ApiOperation("查询客户信息")
    @PostMapping("queryCustomers")
    public CompletableFuture<QueryResponse<QueryPageRes<List<CustomerInfosResDto>>>> queryCustomers(@RequestBody BaseParmDto<QueryCustomersReqDto> request){
        String currentUserId = getCurrentUserId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(currentUserId)){
                    request.getParam().setSaleId(currentUserId);
                    return salesMeetingService.queryCustomers(request);
                }else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/sales/meeting/queryCustomers", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(),e.getMessage());
            }
        });

    }


    /**
     * 发送邀请函
     * @return
     */
    @ApiOperation("发送邀请函")
    @PostMapping("sendInvitation")
    public CompletableFuture<QueryResponse> sendInvitation(@RequestBody @Valid SendInvitationReqDto request, BindingResult result){
        String currentUserId = getCurrentUserId();
        return CompletableFuture.supplyAsync(() -> {
            try {
                if(result.hasErrors()){
                    return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
                }
                if (StringUtils.isNotBlank(currentUserId)) {
                    request.setOpreator(currentUserId);
                    return salesMeetingService.sendInvitation(request);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/sales/meeting/sendInvitation", e);
                return new QueryResponse(ResultStatus.FAILOfficial.getErrorCode(),e.getMessage());
            }
        });
    }


    /**
     * 查看客户回执单信息
     * @param inviteNo 邀约编号
     * @return
     */
    @ApiOperation("查看客户回执单信息")
    @GetMapping("queryReceipt")
    public CompletableFuture<QueryResponse<ReceiptInfoResDto>> queryReceipt(@RequestParam("inviteNo")String inviteNo) {
        String currentUserId = getCurrentUserId();
        return CompletableFuture.supplyAsync(() -> {
            try {
                if (StringUtils.isNotBlank(currentUserId)) {
                    return salesMeetingService.queryReceipt(inviteNo);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/sales/meeting/queryReceipt", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(), e.getMessage());
            }
        });
    }

    /**
     * 接待客户
     * @param code 会议编号
     * @param customerId 客户id
     * @return
     */
    @ApiOperation("接待客户")
    @GetMapping("receptionCustomer")
    public CompletableFuture<QueryResponse<Boolean>> receptionCustomer(@RequestParam("code") String code,
                                                                       @RequestParam("customerId") Long customerId) {
        String currentUserId = getCurrentUserId();
        return CompletableFuture.supplyAsync(() -> {
            try {
                if (StringUtils.isNotBlank(currentUserId)) {
                    return salesMeetingService.receptionCustomer(currentUserId, code, customerId);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/sales/meeting/receptionCustomer", e);
                return new QueryResponse(ResultStatus.FAILOfficial.getErrorCode(), e.getMessage());
            }
        });
    }

    /**
     * 查询销售人员信息
     * @return
     */
    @ApiOperation("查询销售人员信息")
    @GetMapping("querySalesInfo")
    public CompletableFuture<QueryResponse<List<SalesManInfoResDto>>> querySalesInfo(){
        String currentUserId = getCurrentUserId();
        return CompletableFuture.supplyAsync(() -> {
            try {
                if (StringUtils.isNotBlank(currentUserId)) {
                    return salesMeetingService.querySalesInfo(currentUserId);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/sales/meeting/querySalesInfo", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(), e.getMessage());
            }
        });
    }

    /**
     * 转交客户
     * @param cluesId 客户id
     * @param saleId 转交销售id
     * @return
     */
    @ApiOperation("转交客户")
    @GetMapping("handOverCustomer")
    public CompletableFuture<QueryResponse> handOverCustomer(@RequestParam("cluesId") Long cluesId,
                                                             @RequestParam("saleId") Integer saleId) {
        String currentUserId = getCurrentUserId();
        return CompletableFuture.supplyAsync(() -> {
            try {
                if (StringUtils.isNotBlank(currentUserId)) {
                    return salesMeetingService.handOverCustomer(currentUserId, cluesId, saleId);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/sales/meeting/handOverCustomer", e);
                return new QueryResponse(ResultStatus.FAILOfficial.getErrorCode(), e.getMessage());
            }
        });
    }

    /**
     * 新增沟通记录
     * @return
     */
    @ApiOperation("新增沟通记录")
    @PostMapping("addTalkNotes")
    public CompletableFuture<QueryResponse> addTalkNotes(@RequestBody @Valid TalkNotesReqDto request, BindingResult result) {
        String currentUserId = getCurrentUserId();
        return CompletableFuture.supplyAsync(() -> {
            try {
                if(result.hasErrors()){
                    return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
                }
                if (StringUtils.isNotBlank(currentUserId)) {
                    request.setUserId(currentUserId);
                    return salesMeetingService.talkNotes(request);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/sales/meeting/addTalkNotes", e);
                return new QueryResponse(ResultStatus.FAILOfficial.getErrorCode(), e.getMessage());
            }
        });
    }



    /**
     * 获取客户历史沟通记录
     * @param clueGuid
     * @return
     */
    @ApiOperation("获取客户历史沟通记录")
    @GetMapping("getCommunicateList")
    public CompletableFuture<QueryResponse<CommunicateListResDto>> getCommunicateList(@RequestParam("clueGuid")Long clueGuid){
        return CompletableFuture.supplyAsync(() -> {
            try {
                return salesMeetingService.getCommunicateList(clueGuid);
            } catch (Exception e) {
                log.error("/api/sales/meeting/getCommunicateList", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(), e.getMessage());
            }
        });
    }


}
